package com.bo.sina.weibo.login;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.impl.client.DefaultHttpClient;

import com.bo.exception.AppException;
import com.bo.exception.WeiboException;
import com.bo.sina.send.SendPostMessage;
import com.bo.sina.send.WeiboResponse;
import com.bo.sina.weibo.access.AccessInfo;
import com.bo.sina.weibo.name.NameEncodeJSRunner;
import com.bo.sina.weibo.password.PwdEncodeJSRunner;
import com.bo.sina.weibo.sinakey.WeiboKey;

public class Login {
	
	
	
	
	public  WeiboResponse loginWeibo(AccessInfo accessinfo) throws AppException {
		Map<String,String> paramsMap=new HashMap<String,String>();
		DefaultHttpClient client = new DefaultHttpClient();
		try {
			WeiboKey weibokey=new WeiboKey();
			HashMap<String, String> params=weibokey.preLogin(accessinfo.getName(), client);
			//密码加密
			PwdEncodeJSRunner pwdEncodeJSRunner = new PwdEncodeJSRunner();
			String pass = pwdEncodeJSRunner.executeFunction(params.get("pubkey"), params.get("servertime"), params.get("nonce"), accessinfo.getPassword());
			
			//用户名加密
		    NameEncodeJSRunner njsrunner = new NameEncodeJSRunner();
			String su = njsrunner.executeFunction(accessinfo.getName());
//			System.out.println("sp = " + pass);
//			System.out.println("su = " + su);
			


			paramsMap.put("encoding", "UTF-8");
			paramsMap.put("entry", "weibo");
			paramsMap.put("from", "");
			paramsMap.put("gateway", "1");
			paramsMap.put("pagerefer", "http://i.firefoxchina.cn/old/");
			paramsMap.put("prelt", "111");
			paramsMap.put("pwencode", "rsa2");
			paramsMap.put("returntype", "META");
			paramsMap.put("savestate", "0");
			paramsMap.put("service", "miniblog");
			paramsMap.put("useticket", "1");
			paramsMap.put("vsnf", "1");
			paramsMap.put("url","http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack");
			
			paramsMap.put("rsakv", params.get("rsakv"));
			paramsMap.put("servertime", params.get("servertime"));
			paramsMap.put("nonce", params.get("nonce"));
			paramsMap.put("sp",pass);
			paramsMap.put("su", su);
			
			
			Map<String,String> requestMap=new HashMap<String,String>();
			requestMap.put("Referer","http://weibo.com/?c=spr_web_sq_firefox_weibo_t001");
			
			SendPostMessage spm=new SendPostMessage();
			WeiboResponse entity = spm.send("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)", "GBK",paramsMap,requestMap);


			
			if (entity.getMessage().replace("\"", "").indexOf("retcode=0") > -1) {
//				System.out.println(new String(entity.getMessage().getBytes("ISO-8859-1"),"GBK"));
//				 System.out.println("登录成功");
				 return entity;
			}else{
				throw new AppException("511","用户名："+accessinfo.toString()+"  "+entity.getMessage());
			}
		} catch (Exception e) {
			WeiboException.addLoginExceptionNo();
			throw new AppException("511",e);
		}
	}
	
//
//	
//	
//	public  String login(String u, String p) {
//		DefaultHttpClient client = new DefaultHttpClient();
//		try {
//			WeiboKey weibokey=new WeiboKey();
//			HashMap<String, String> params=weibokey.preLogin(u, client);
//			
//			
//			HttpPost post = new HttpPost("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)");
//			post.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//			post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
//			post.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
//			post.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
//			post.setHeader("Referer","http://weibo.com/?c=spr_web_sq_firefox_weibo_t001");
//			post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//
//			//密码加密
//			PwdEncodeJSRunner pwdEncodeJSRunner = new PwdEncodeJSRunner();
//			String pass = pwdEncodeJSRunner.executeFunction(params.get("pubkey"), params.get("servertime"), params.get("nonce"), p);
//			
//			//用户名加密
//		    NameEncodeJSRunner njsrunner = new NameEncodeJSRunner();
//			String su = njsrunner.executeFunction(u);
//			System.out.println("sp = " + pass);
//			System.out.println("su = " + su);
//
//			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//			nvps.add(new BasicNameValuePair("encoding", "UTF-8"));
//			nvps.add(new BasicNameValuePair("entry", "weibo"));
//			nvps.add(new BasicNameValuePair("from", ""));
//			nvps.add(new BasicNameValuePair("gateway", "1"));
//			nvps.add(new BasicNameValuePair("pagerefer", "http://i.firefoxchina.cn/old/"));
//			nvps.add(new BasicNameValuePair("prelt", "111"));
//			nvps.add(new BasicNameValuePair("pwencode", "rsa2"));
//			nvps.add(new BasicNameValuePair("returntype", "META"));
//			nvps.add(new BasicNameValuePair("savestate", "0"));
//			nvps.add(new BasicNameValuePair("service", "miniblog"));
//			nvps.add(new BasicNameValuePair("useticket", "1"));
//			nvps.add(new BasicNameValuePair("vsnf", "1"));
//			nvps.add(new BasicNameValuePair("url",
//					"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"));
//			
//			nvps.add(new BasicNameValuePair("rsakv", params.get("rsakv")));
//			nvps.add(new BasicNameValuePair("servertime", params.get("servertime")));
//			nvps.add(new BasicNameValuePair("nonce", params.get("nonce")));
//			nvps.add(new BasicNameValuePair("sp",pass));
//			nvps.add(new BasicNameValuePair("su", su));
//			
//			
//
//			post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
//
//			HttpResponse response = client.execute(post);
//
//			String entity = EntityUtils.toString(response.getEntity());
////			for(Header header:response.getAllHeaders()){
////				System.out.println(header.toString());
////			}
//			if (entity.replace("\"", "").indexOf("retcode=0") > -1) {
//				System.out.println(new String(entity.getBytes("ISO-8859-1"),"GBK"));
//				 System.out.println("登录成功");
//				 return entity;
//			}
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		return null;
//	}
//	
//	public String getUid(String entity) throws ClientProtocolException, IOException{
//		HttpClient client = new DefaultHttpClient(
//				new ThreadSafeClientConnManager());
//		
//		String url = entity.substring(entity
//				.indexOf("http://weibo.com/sso/login.php?"), entity
//				.indexOf("code=0")+6 );
//
//		// 获取到实际url进行连接
//		HttpGet getMethod = new HttpGet(url);
//		HttpResponse response = client.execute(getMethod);
//		entity = EntityUtils.toString(response.getEntity());
//		System.out.println(entity);
//
//		// 获取uid
//		String uid = entity.substring(entity.indexOf("uniqueid") + 11,
//				entity.indexOf("userid") - 3);
////		Share share=new Share();
////		share.Share(uid);
//		return uid;
//	}
//	

}
